/*
 * @描述: 权限管理的数据仓库
 * @Author: /songzi
 * @Date: 2020-01-16 14:48:53
 * @Last Modified by: songzi
 * @Last Modified time: 2020-01-16 15:50:49
 */

import {
  queryPermissionsInfo,
  queryPermissionsList,
  queryPermissionsTypeList,
  addPermission,
  editPermission,
  deletePermission,
  queryAuthApis
} from '@/services/authority';

import {
  ADD,
  EDIT,
  DELETE,
  AUTHORITY,
  GET_PERMISSION_LIST,
  SET_PERMISSION_LIST,
  GET_PERMISSION_INFO,
  SET_PERMISSION_INFO,
  GET_PERMISSION_TYPE_LIST,
  SET_PERMISSION_TYPE_LIST
} from '@/actionConstant';

import { Auth } from '@/utils/MFAuth';

export default {
  namespace: AUTHORITY,
  state: {
    authApis: [],
    permissionsList: [],
    permissionsInfo: {},
    permissionsTypeList: [
      {
        code: 'MENU',
        name: '菜单'
      },
      {
        code: 'PAGE',
        name: '页面'
      },
      {
        code: 'GROUP',
        name: '分组'
      },
      {
        code: 'LIST',
        name: '列表'
      },
      {
        code: 'INTERFACE',
        name: '接口'
      },
      {
        code: 'ELEMENT',
        name: '元素'
      }
    ]
  },

  effects: {
    *[ADD]({ payload }, { call, put }) {
      const response = yield call(addPermission, payload);
      return response;
    },
    *[EDIT]({ payload }, { call, put }) {
      const response = yield call(editPermission, payload);
      return response;
    },
    *[DELETE]({ payload }, { call, put }) {
      const response = yield call(deletePermission, payload);
      return response;
    },
    *[GET_PERMISSION_LIST]({ payload = {} }, { call, put }) {
      const response = yield call(queryPermissionsList, payload);
      yield put({
        type: SET_PERMISSION_LIST,
        payload: response.data
      });
    },
    *[GET_PERMISSION_INFO]({ payload = {} }, { call, put }) {
      const response = yield call(queryPermissionsInfo, payload);
      yield put({
        type: SET_PERMISSION_INFO,
        payload: response.data
      });
    },
    *[GET_PERMISSION_TYPE_LIST]({ payload = {} }, { call, put }) {
      const response = yield call(queryPermissionsTypeList, payload);
      yield put({
        type: SET_PERMISSION_TYPE_LIST,
        payload: response.data
      });
    },
    *getAuthApis({ payload = {} }, { call, put }) {
      const response = yield call(queryAuthApis, payload);
      const { data } = response || {};
      Auth.setApis(data || []);
      yield put({
        type: 'setAuthApis',
        payload: data || []
      });
    }
  },

  reducers: {
    [SET_PERMISSION_LIST](state, action) {
      console.log(action, 'actionactionactionaction');
      return {
        ...state,
        permissionsList: action.payload || []
      };
    },
    [SET_PERMISSION_INFO](state, action) {
      return {
        ...state,
        permissionsInfo: action.payload || {}
      };
    },
    [SET_PERMISSION_TYPE_LIST](state, action) {
      return {
        ...state,
        permissionsTypeList: action.payload
      };
    },
    setAuthApis(state, action) {
      return {
        ...state,
        authApis: action.payload
      };
    }
  }
};
